package ie.dcu.spca.data.db;

import java.util.List;

import ie.dcu.spca.data.CategoryDataManager;
import ie.dcu.spca.model.Bean;
import ie.dcu.spca.model.bean.Category;
import ie.dcu.spca.model.dao.CategoryDao;

public class DBCategoryDataManager extends DBBasicDataManager implements
        CategoryDataManager {

    private static CategoryDao cDao = new CategoryDao();
    private List<Category> categoriesCache = null;

    private CategoryDao categoryDao() {
        categoriesCache = null;
        return cDao;
    }

    public DBCategoryDataManager() {
        super();
    }

    @Override
    public int create(String name) {
        return categoryDao().insertCategory(name);
    }

    @Override
    public List<Category> get() {
        if (categoriesCache == null)
            categoriesCache = (List) categoryDao().selectAllBean();
        return categoriesCache;
    }

    @Override
    public void update(int id, String name) {
        Category c = (Category) categoryDao().selectById(id);
        c.setName(name);
        categoryDao().updateByBean(c);
    }

    @Override
    public void delete(int id) {
        categoryDao().deleteById(id);
    }

    @Override
    public List<Bean> getBeanList() {
        return (List) get();
    }

    @Override
    public int getId(Bean bean) {
        return ((Category) bean).getId();
    }

    @Override
    public String getName(Bean bean) {
        return ((Category) bean).getName();
    }

    @Override
    public int getLastId() {
        return categoryDao().selectMaxId();
    }

}
